// Basic DOM shortcut functions
function getEl(x) {
    return document.getElementById(x)
}
function ctn(x) {
    return document.createTextNode(x)
}
function cel(x) {
    return document.createElement(x)
}
function addEvent(obj, type, fn) {
    if (obj.addEventListener) {
        obj.addEventListener(type, fn, false)
    }
    else if (obj.attachEvent) {
        obj["e" + type + fn] = fn;
        obj.attachEvent("on" + type, function() {
            obj["e" + type + fn]();
        });
    }
}

// JSON ScriptBuilder Function and Prototype, from Jason Levitt of Yahoo!
// code found here: http://devx.com/webdev/Article/30860/1954
function JSONscriptRequest(fullUrl) {
    this.fullUrl = fullUrl;
    this.noCacheIE = '&noCacheIE=' + (new Date()).getTime();
    this.headLoc = document.getElementsByTagName("head").item(0);
    this.scriptId = 'azScriptId' + JSONscriptRequest.scriptCounter++;
}
JSONscriptRequest.scriptCounter = 1;
JSONscriptRequest.prototype.buildScriptTag = function () {
    this.scriptObj = document.createElement("script");
    this.scriptObj.setAttribute("type", "text/javascript");
    this.scriptObj.setAttribute("src", this.fullUrl + this.noCacheIE);
    this.scriptObj.setAttribute("id", this.scriptId);
}
JSONscriptRequest.prototype.removeScriptTag = function () {
    this.headLoc.removeChild(this.scriptObj);
}
JSONscriptRequest.prototype.addScriptTag = function () {
    this.headLoc.appendChild(this.scriptObj);
}

// Callback function referenced by the JSON script, This teases the
// JSON object out into DOM Nodes, and appends them to 'searchResults' DIV)
var amzJSONCallback = function(tmpData) {
    var count = 0;
    var ol = cel('ol');
    var pubDate;
    var gDiv = getEl('searchResults');
    gDiv.innerHTML = "";
    if (tmpData.ItemSet.Item.length > 4) {
        upperLimit = 5
    } else {
        upperLimit = tmpData.ItemSet.Item.length - 1
    }
    for (i = 0; i < upperLimit; i++) {
        count++;
        var tmpItem = tmpData.ItemSet.Item[i]
        var li = cel('li');
        var a = cel('a');
        a.setAttribute('href', tmpItem.url);
        a.appendChild(ctn(tmpItem.title));
        li.appendChild(a);
        if (tmpItem.price) {
            li.appendChild(ctn(" - " + tmpItem.price));
        } else if (tmpItem.lowestusedprice) {
            li.appendChild(ctn(" - " + tmpItem.lowestusedprice + " (used)"));
        }
        ol.appendChild(li);
    }
    if (count != 0) {
        gDiv.appendChild(ol);
    }
}

// Search function - creates, builds, and adds remote JSON script
// to the page, which in turn calls amzJSONCallback
var amazonSearch = function(gbAmazonSearch) {
    var request = 'http://xml-us.amznxslt.com/onca/xml?Service=AWSECommerceService&SubscriptionId=19267494ZR5A8E2CGPR2&AssociateTag=kokogiak7-20&Operation=ItemSearch&Style=http://kokogiak.com/amazon/JSON/ajsonCategorySearch.xsl&ContentType=text/javascript&ItemPage=1&SearchIndex=All&ResponseGroup=Small,Offers&MerchantId=ATVPDKIKX0DER&Availability=Available&Condition=All&Keywords=' + gbAmazonSearch + '&CallBack=amzJSONCallback';
    aObj = new JSONscriptRequest(request);
    aObj.buildScriptTag();
    aObj.addScriptTag();
}
